package com.dic.service;

import java.util.HashMap;
import java.util.List;
import java.util.HashMap;
import org.apache.ibatis.session.SqlSession;

import com.dic.dao.UserDao;
import com.dic.dao.model.User;
import com.dic.util.SqlMapperFactory;

public class UserService {

	public User getUser(int id){  //getUser()����
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		User user = null;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			user = userDao.getUser(id);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}
	
	public User checkExist(User user1){//  //�ж��û��Ƿ��Ѵ��ڣ�ͬʱƥ���û������룬Ȩ��
		//System.out.println(user1.getUsername()+":"+user1.getPassword()+":"+user1.getRole());
		//System.out.println("aa"+username+":"+password+":"+role);
		SqlSession session = null;
		session = SqlMapperFactory.getFactory().openSession();
		
		User user2=null;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			user2= userDao.checkExist(user1);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user2;
	}
	
	public User checkExist2(HashMap hm){ //�鿴�û��Ƿ���ڣ�ƥ���û����Ȩ��~����
		SqlSession session = null;
		session = SqlMapperFactory.getFactory().openSession();
		User user2=null;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			user2= userDao.checkExist2(hm);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user2;
	}
	
	public int insertUser(User user){  //�������û���������֮ǰ��Ҫ������ݿ����Ƿ��Ѵ��ڸ��û�
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		int insertflag=0;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			insertflag=userDao.insertUser(user);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return insertflag;
	}
	public User getPassword(HashMap hm){
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		User user=null;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			user=userDao.getPassword(hm);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}
	public int updatePw(User user){  //��������
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		int updateflag=0;
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			updateflag=userDao.updatePw(user);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return updateflag;
	}
	public User isExist(String username){  //������ݿ����Ƿ��Ѵ��ڸ��û���ֻ���û���~
		User user=null;
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			user = userDao.isExist(username);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}
	public List<String> getPermissions(String role){
		List<String> lists=null;
		SqlSession session = SqlMapperFactory.getFactory().openSession();
		try {
			UserDao userDao = session.getMapper(UserDao.class);
			lists = userDao.getPermissions(role);
			session.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return lists;
		
	}
	
	
}
